package com.fy.tt.executor.node.sentence.simpleStmt;

import com.fy.tt.action.TemplateTool;
import com.fy.tt.executor.environment.threadPool.ThreadPoolGroup;
import com.fy.tt.executor.node.script.SimpleStmt;
import com.fy.tt.executor.node.word.noun.Band;
import com.fy.tt.parser.V3Parser;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.concurrent.Future;

public class ListenStmt extends SimpleStmt {

    ThreadPoolGroup threadPoolGroup = null;

    {
        try {
            threadPoolGroup = TemplateTool.getBean(ThreadPoolGroup.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private Band band;
    private String routingKey;
    private String typesMessage;
    private IntenseStmt intenseStmt;

    public ListenStmt(V3Parser.ListenStmtContext ctx, Map<String, Object> scriptEnvironment) {
        String bandName = ctx.place().Identifier().getText();
        band = new Band(bandName, scriptEnvironment);
        typesMessage = ctx.Identifier().getText();

        if (typesMessage.startsWith("\"")) {
            typesMessage = typesMessage.substring(1, typesMessage.length() - 1);
        } else {
            typesMessage = typesMessage.trim();
        }

        routingKey = band.getBandID() + "." + typesMessage;

        if(ctx.intenseStmt() != null && ctx.intenseStmt().runToolStmt() != null){
            if (ctx.intenseStmt().runToolStmt().notifyStmt() == null) {
                intenseStmt = new RunToolStmt(ctx.intenseStmt().runToolStmt(), scriptEnvironment);
            } else {
                intenseStmt = new NotifyStmt(ctx.intenseStmt().runToolStmt().notifyStmt(), scriptEnvironment);
            }
        }

    }

    public Future<?> runTask() {
        Future<?> task = null;
//        try {
//            ThreadPoolExecutor executor = threadPoolGroup.getListenThreadPool();
//            MyMQ mq = ActionTool.getBean(MyMQ.class);
//            task = executor.submit(() -> {
//                System.out.println("thread id is: " + Thread.currentThread().getId());
//                try {
//                    String EXCHANGE_NAME = "item_topic_exchange";//交换机名称，写死
//                    String queueName = "item_queue";//要监听的队列名称，可以用时间戳+随机数
//                    mq.channel.queueDeclare(queueName, false, false, false, null);
//                    mq.channel.queueBind(queueName, EXCHANGE_NAME, routingKey);//定义要监听的路由类型
//                    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
//                        String receivedMessage = new String(delivery.getBody(), "utf-8");
//                        System.out.println("收到消息为:" + receivedMessage + ",路由为:" + routingKey);
//                        intenseStmt.run("");
//                    };
//                    mq.channel.basicConsume(queueName, true, deliverCallback, (consumerTag) -> {
//                        System.out.println("取消接受信息");
//                    });
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            });
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return task;
    }

}
